﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using OneWaySoapChannelLib.Binding;

namespace OneWaySoapChannelLib.Channel
{
    public class SoapStoreChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
    {
        OneWaySoapBindingElement _element = null;
        BindingContext _context = null;

        public SoapStoreChannelFactory() : base()
        {
        }

        public SoapStoreChannelFactory(OneWaySoapBindingElement element, BindingContext context)
            : base(context.Binding)
        {
            _element = element;
            _context = context;
        }

        protected override TChannel OnCreateChannel(EndpointAddress address, Uri via)
        {
            if (!string.Equals(address.Uri.Scheme, _element.Scheme, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ArgumentException(string.Format("The scheme {0} specified in address is not supported.", address.Uri.Scheme), "remoteAddress");
            }

            if (typeof(TChannel) == typeof(IOutputChannel))
                return (TChannel)(object)new OneWaySoapOutputChannel(this, address, via, _element.ConnectionString);

            throw new InvalidOperationException("Channel type not supported");
        }

        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            throw new NotImplementedException();
        }

        protected override void OnEndOpen(IAsyncResult result)
        {
            throw new NotImplementedException();
        }

        protected override void OnOpen(TimeSpan timeout)
        {           
        }
          
        protected override void OnClosed()
        {
            base.OnClosed();
        }
    }
}
